#include "Scintilla.h"
#include <QtDebug>
Scintilla::Scintilla(QWidget* parent):QsciScintilla(parent)
{
	PopMenu  = new QMenu(this);
    QColor c(231,34,100);
}

Scintilla::~Scintilla(void)
{
	delete PopMenu;
}

void Scintilla::CreateMenu()
{
   // SendScintilla(SCI_STYLESETBACK, STYLE_DEFAULT, QColor(220,220,220));
   //SendScintilla(SCI_SETSELFORE,true,QColor(250,0,0));
    const char* ssss ="1332";
   SendScintilla(SCI_STYLESETFORE,STYLE_DEFAULT,QColor(250,0,0));
   SendScintilla(SCI_SETTEXT,ssss);
   SendScintilla(SCI_SETSTYLEBITS,5);
   SendScintilla(SCI_STARTSTYLING,1,0x0F);
   SendScintilla(SCI_SETSTYLING,1,STYLE_DEFAULT);

	PopMenu->clear();
	ActCopy = new QAction(tr("Copy"),this);
	connect(ActCopy,SIGNAL(triggered()),this,SLOT(copy()));
	ActCut = new QAction(tr("Cut"),this);
	connect(ActCut,SIGNAL(triggered()),this,SLOT(cut()));
	ActPaste = new QAction(tr("Paste"),this);
	connect(ActPaste,SIGNAL(triggered()),this,SLOT(paste()));
	ActDelete = new QAction(tr("Delete"),this);
	connect(ActDelete,SIGNAL(triggered()),this,SLOT(removeSelectedText()));
	ActToggleComment = new QAction(tr("Toggle Comment"),this);
	connect(ActToggleComment,SIGNAL(triggered()),this,SIGNAL(ToggleCommentSignal()));
	ActBlockComment = new QAction(tr("Block Comment"),this);
	connect(ActBlockComment ,SIGNAL(triggered()),this,SIGNAL(BlockCommentSignal()));	
	ActBlockStream = new QAction(tr("Block Stream"),this);
	connect(ActBlockStream,SIGNAL(triggered()),this,SIGNAL(BlockStreamSignal()));
	ActUnComment = new QAction(tr("UnComment"),this);
	connect(ActUnComment,SIGNAL(triggered()),this,SIGNAL(UnCommentSignal()));


	int Line,Index;
	getCursorPosition(&Line,&Index);
	QString LineText = text(Line);
	QRegExp FindHeader("^[ \t]*#[ \t]*include[ \t]+[\"<]([^\">]+)[\">]");
	int Result = FindHeader.indexIn(LineText);
	if(Result > -1)
	{
		MatchText = FindHeader.cap(1);
		ActHeader = new QAction(tr("Open File:%1").arg(MatchText),this);
		connect(ActHeader,SIGNAL(triggered()),this,SLOT(SendFileName()));
		PopMenu->addAction(ActHeader);
		PopMenu->addSeparator();
	}
	else
	{

		KeyText = GetTextRange();
		if(!KeyText.isEmpty())
		{
			ActDeclaration  = new QAction(tr("Find Declaration: %1").arg(KeyText),this);
			ActImplementation = new QAction(tr("Find Implementation: %1").arg(KeyText),this);
			connect(ActDeclaration,SIGNAL(triggered()),this,SLOT(SendTextForDeclaration()));
			connect(ActImplementation,SIGNAL(triggered()),this,SLOT(SendTextForImplementation()));
			PopMenu->addAction(ActDeclaration);
			PopMenu->addAction(ActImplementation);
			PopMenu->addSeparator();
		}
	}
	PopMenu->addAction(ActToggleComment);
	PopMenu->addAction(ActBlockComment);
	PopMenu->addAction(ActBlockStream);
	PopMenu->addAction(ActUnComment);
	PopMenu->addSeparator();
	PopMenu->addAction(ActCopy);
	PopMenu->addAction(ActCut);
	PopMenu->addAction(ActPaste);
	PopMenu->addAction(ActDelete);
}

void Scintilla::mousePressEvent (QMouseEvent *e )
{
	if( e->button() == Qt::LeftButton)
        {
                //do something ??
        }
        else if( e->button() == Qt::RightButton)
        {	
                CreateMenu();
                QPoint pos = e->pos();
                pos = mapToGlobal(pos);
                PopMenu->popup(pos);
        }
        QsciScintilla::mousePressEvent(e);
}

/*
    the code from QsciScintilla
*/

QString Scintilla::GetTextRange()
{
	int start,ignore;
	QStringList context = apiContext(SendScintilla(SCI_GETCURRENTPOS), start, ignore);
    int caret = SendScintilla(SCI_GETCURRENTPOS);
    SendScintilla(SCI_STYLEGETSIZE,20);
    int clen = caret - start;
    char *orig_context = new char[clen + 1];
    SendScintilla(SCI_GETTEXTRANGE, start, caret, orig_context);
	QString Qs = convertTextS2Q(orig_context);
	delete[] orig_context;
    qDebug()<<Qs;
	return Qs;
}

void Scintilla::SendFileName(void)
{
	emit SendFileNameSignal(MatchText);
}

void Scintilla::SendTextForDeclaration(void)
{
	emit SendTextForDecSignal(KeyText);
}

void Scintilla::SendTextForImplementation(void)
{
	emit SendTextForImpSignal(KeyText);
}


QString Scintilla::convertTextS2Q(const char *s) const
{
    if (isUtf8())
        return QString::fromUtf8(s);
    return QString::fromLatin1(s);
}

bool Scintilla::isUtf8() const
{
    return (SendScintilla(SCI_GETCODEPAGE) == SC_CP_UTF8);
}
/*
	end
*/
